package com.hao.staservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hao.staservice.client.UcenterClient;
import com.hao.staservice.entity.StatisticsDaily;
import com.hao.staservice.mapper.StatisticsDailyMapper;
import com.hao.staservice.service.StatisticsDailyService;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author hao
 * @since 2021-01-27
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UcenterClient ucenterClient;

    @Override
    public void registerCount(String day) {
        //先删除当天已存在的统计数据
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper();
        wrapper.eq("date_calculated", day);
        this.remove(wrapper);
        //再添加最新数据
        StatisticsDaily daily = new StatisticsDaily();
        Integer countRegister = ucenterClient.countRegister(day);
        daily.setRegisterNum(countRegister);
        daily.setDateCalculated(day);
        //模拟数据
        daily.setCourseNum(RandomUtils.nextInt(100, 200));
        daily.setLoginNum(RandomUtils.nextInt(100, 200));
        daily.setVideoViewNum(RandomUtils.nextInt(100, 200));
        //将数据存储
        this.save(daily);
    }

    @Override
    public Map<String, Object> showData(String type, String beginDay, String endDay) {
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper();
        //查询需要的列
        wrapper.select("date_calculated", type);
        //查询两个日期之间数据
        wrapper.between("date_calculated", beginDay, endDay);
        List<StatisticsDaily> list = this.list(wrapper);
        //结果封装为list集合，传递前端会转为数组json格式
        //返回两部分数据，日期，和日期对应的数量
        List<String> dateList = new ArrayList<>();
        List<Integer> numsList = new ArrayList<>();
        for (StatisticsDaily daily : list) {
            dateList.add(daily.getDateCalculated());
            switch (type) {
                case "login_num":
                    numsList.add(daily.getLoginNum());
                    break;
                case "register_num":
                    numsList.add(daily.getRegisterNum());
                    break;
                case "video_view_num":
                    numsList.add(daily.getVideoViewNum());
                    break;
                case "course_num":
                    numsList.add(daily.getCourseNum());
                    break;
                default:
                    break;
            }
        }
        Map<String, Object> map = new HashMap<>(16);
        map.put("numDataList", numsList);
        map.put("date_calculatedList", dateList);
        return map;

    }
}
